package com.ruoyi.swzl.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Email;

import com.ruoyi.common.utils.email.EmailUtil;
import com.ruoyi.common.utils.email.Mail;
import com.ruoyi.swzl.domain.SendEmailRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.swzl.domain.Goods;
import com.ruoyi.swzl.service.IGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物品遗失/拾取登记Controller
 * 
 * @author k
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/register/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    @Resource
    private EmailUtil emailUtil;

    @PostMapping("/send/email")
    public AjaxResult sendEmail(@RequestBody SendEmailRequest request){
        if (request.getGoodsId() == null)
        {
            return AjaxResult.error("物品数据异常，稍后重试");
        }
        Goods goods = goodsService.selectGoodsById(request.getGoodsId());
        String receiveEmail = goods.getEmail();
        Mail mail = new Mail();
        mail.setRecipient(receiveEmail);
        mail.setContent(request.getMessage()+request.getEmail());
        emailUtil.sendSimpleMail(mail);
        return AjaxResult.success();
    }

    /**
     * 查询物品遗失/拾取登记列表
     */
    @PreAuthorize("@ss.hasPermi('register:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    @GetMapping("/get/all")
    public AjaxResult getAll(String type){
        long goodsType = 0;
        if (type != null){
            switch (type) {
                case "all":
                    return AjaxResult.success(goodsService.selectGoodsList(null));
                case "dianzi":
                    goodsType = 2L;
                    break;
                case "guizhong":
                    goodsType = 0L;
                    break;
                case "other":
                    goodsType = 4L;
                    break;
            }
        }
        Goods goods = new Goods();
        goods.setGoodsType(goodsType);
        return AjaxResult.success(goodsService.selectGoodsList(goods));
    }

    /**
     * 导出物品遗失/拾取登记列表
     */
    @PreAuthorize("@ss.hasPermi('register:goods:export')")
    @Log(title = "物品遗失/拾取登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "物品遗失/拾取登记数据");
    }

    /**
     * 获取物品遗失/拾取登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('register:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(goodsService.selectGoodsById(id));
    }

    /**
     * 新增物品遗失/拾取登记
     */
    @PreAuthorize("@ss.hasPermi('register:goods:add')")
    @Log(title = "物品遗失/拾取登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改物品遗失/拾取登记
     */
    @PreAuthorize("@ss.hasPermi('register:goods:edit')")
    @Log(title = "物品遗失/拾取登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除物品遗失/拾取登记
     */
    @PreAuthorize("@ss.hasPermi('register:goods:remove')")
    @Log(title = "物品遗失/拾取登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }
}
